var libs = require('libs/libs');
var qs = require('querystring')

function replaceState(tag){
    var url = libs.urlparse(location.href);
    var params = url.params;
    if (params[tag]){
        var _src = url.relative.replace(tag+'='+url.params[tag], '').replace('?&', '?').replace('?#','#').replace('&&', '&').replace('&#', '#')
        history.replaceState(null,null, _src)
        setTimeout(function(){ history.replaceState(null,null, _src) }, 0)
    }
}

function reState(tag, value){
    var url = libs.urlparse(location.href);
    var params = url.params;
    params[tag] = value;
    var rct = qs.stringify(params)
    rct = url.relative.replace(url.path, url.path+'?'+rct)
    // console.log(url);
    // alert(rct)
    history.replaceState(null,null, rct)
}

module.exports = {
    replaceState: replaceState,
    reState: reState
}
